/* 
 * File:   Result.cpp
 * Author: Denys Knertser
 * 
 * Created on February 14, 2012, 2:16 PM
 */
using namespace std;

#include "Result.h"

Result::Result(string strPack, string secret, string challenge, char prev_id) : PacketFormat(strPack) {
    value_size=data[0];
    value=data.substr(1,value_size);
    name=data.substr(value_size+1,string::npos); //checked to be "client"
    data="";
    success=false;
    if (prev_id==identifier)
        checkResponse(secret,challenge);
    code=success?3:4;
    length=data.length();
}

int Result::checkResponse(string secret, string challenge) {
    string buf=string(1,identifier)+secret+challenge;
    buf=string((const char*)MD5((const unsigned char*)buf.c_str(),buf.length(),NULL),16);
    success=(buf==value) && (name=="client");
    return 0;
}
